﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;



using HGZP.PLL;


namespace HGZP.DAL
{
    static public class GetDataStructure
    {
        static private DataSet dataStructureAllTable = null;
        static private DataSet dataStructureAllColumn = null;

        static public DataSet DataStructureAllTable
        {
            get { GetDataBaseStructure(); return dataStructureAllTable; }
            set { dataStructureAllTable = value; }
        }
        static public DataSet DataStructureAllColumn
        {
            get { GetDataBaseStructure(); return dataStructureAllColumn; }
            set { dataStructureAllColumn = value; }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        static public void GetDataBaseStructure()
        {
            //
            if (dataStructureAllTable != null && dataStructureAllColumn != null)
            {
                return;
            }
            //获取Sqlserver的版本
            SqlHelp mySqlHelp = new SqlHelp();
            DataSet dsSqlVersion = mySqlHelp.SelectDataBySql(" select @@version ");
            if (dsSqlVersion == null) { throw new Exception(SqlHelp.globalErrorString); }
            MyEnum.SqlServerType sqlVersion = MyEnum.SqlServerType.Sql2005;//默认是sql2005
            if (dsSqlVersion.Tables[0].Rows.Count != 0)
            {
                if (dsSqlVersion.Tables[0].Rows[0][0].ToString().IndexOf("2000") >= 0) { sqlVersion = MyEnum.SqlServerType.Sql2000; }
            }
            string sSQlGetTable = string.Empty;
            string sSQlGetColumn = string.Empty;
            if (sqlVersion == MyEnum.SqlServerType.Sql2000)
            {
                sSQlGetTable = sSqlTable2000;
                sSQlGetColumn = sSqlColumn2000;
            }
            else
            {
                sSQlGetTable = sSqlTable2005;
                sSQlGetColumn = sSqlColumn2005;
            }
            //
            //
            dataStructureAllTable = mySqlHelp.SelectDataBySql(sSQlGetTable);
            if (dataStructureAllTable == null)
            {
                throw new Exception(SqlHelp.globalErrorString);
            }
            #region 查询数据库结构
            dataStructureAllColumn = mySqlHelp.SelectDataBySql(sSQlGetColumn);
            if (dataStructureAllColumn == null) { throw new Exception(SqlHelp.globalErrorString); }
            #endregion 查询数据库结构
            //
            //return true;
            //
        }

        #region sSqlTable2000
        static private string sSqlTable2000 = @"
        SELECT
        
        d.name AS sTableID, f.value as sTableName
        
        FROM syscolumns a
        
        left join systypes b on a.xusertype=b.xusertype
        
        inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
        
        left join syscomments e on a.cdefault=e.id
        
        left join sysproperties g on a.id=g.id and a.colid=g.smallid
        
        left join sysproperties f on d.id=f.id and f.smallid=0
        group by d.name
                                                  ";
        #endregion
        #region sSqlColumn2000


        static private string sSqlColumn2000 = @"
           SELECT
        
        表名= d.name ,
        
        表说明=f.value ,
        
        字段序号=a.colorder,
        
        字段名=a.name,
        
        标识=case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end,
        
        主键=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and name in (
        
        SELECT name FROM sysindexes WHERE indid in(
        
        SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid
        
        ))) then '√' else '' end,
        
        类型=b.name,
        
        占用字节数=a.length,
        
        精度=COLUMNPROPERTY(a.id,a.name,'PRECISION'),
        
        小数位数=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
        
        允许空=case when a.isnullable=1 then '√'else '' end,
        
        默认值=isnull(e.text,''),
        
        字段说明=isnull(g.[value],'')
      
        FROM syscolumns a
        
        left join systypes b on a.xusertype=b.xusertype
        
        inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
        
        left join syscomments e on a.cdefault=e.id
        
        left join sysproperties g on a.id=g.id and a.colid=g.smallid
        
        left join sysproperties f on d.id=f.id and f.smallid=0

  where (b.name <> 'timestamp' )
        ORDER BY d.name, 字段序号
                                                  ";
        #endregion
        //
        #region sSqlTable2005


        static private string sSqlTable2005 = @"
                         SELECT 
                              distinct d.name  AS sTableID,
                              f.value as sTableName
                        FROM dbo.syscolumns a LEFT OUTER JOIN
                              dbo.systypes b ON a.xtype = b.xusertype INNER JOIN
                              dbo.sysobjects d ON a.id = d.id AND d.xtype = 'U' AND 
                              d.status >= 0 LEFT OUTER JOIN
                              dbo.syscomments e ON a.cdefault = e.id LEFT OUTER JOIN
                              sys.extended_properties g ON a.id = g.major_id AND a.colid = g.minor_id AND 
                              g.name = 'MS_Description' LEFT OUTER JOIN
                              sys.extended_properties f ON d.id = f.major_id AND f.minor_id = 0 AND 
                              f.name = 'MS_Description'
                        
                            order by d.name 
                                                  ";
        #endregion
        #region sSqlColumn2005


        static private string sSqlColumn2005 = @"
                       SELECT TOP 100 PERCENT --a.id, 
                      d.name  AS 表名, 
                      f.value AS 表说明, 
                      a.colorder AS 字段序号, a.name AS 字段名, CASE WHEN COLUMNPROPERTY(a.id, 
                      a.name, 'IsIdentity') = 1 THEN '√' ELSE '' END AS 标识, 
                      CASE WHEN EXISTS
                          (SELECT 1
                         FROM dbo.sysindexes si INNER JOIN
                               dbo.sysindexkeys sik ON si.id = sik.id AND si.indid = sik.indid INNER JOIN
                               dbo.syscolumns sc ON sc.id = sik.id AND sc.colid = sik.colid INNER JOIN
                               dbo.sysobjects so ON so.name = si.name AND so.xtype = 'PK'
                         WHERE sc.id = a.id AND sc.colid = a.colid) THEN '√' ELSE '' END AS 主键, 
                      b.name AS 类型, a.length AS 长度, COLUMNPROPERTY(a.id, a.name, 'PRECISION') 
                      AS 精度, ISNULL(COLUMNPROPERTY(a.id, a.name, 'Scale'), 0) AS 小数位数, 
                      CASE WHEN a.isnullable = 1 THEN '√' ELSE '' END AS 允许空, ISNULL(e.text, '') 
                      AS 默认值, ISNULL(g.[value], '') AS 字段说明, d.crdate AS 创建时间, 
                      CASE WHEN a.colorder = 1 THEN d.refdate ELSE NULL END AS 更改时间
                FROM dbo.syscolumns a LEFT OUTER JOIN
                      dbo.systypes b ON a.xtype = b.xusertype INNER JOIN
                      dbo.sysobjects d ON a.id = d.id AND d.xtype = 'U' AND 
                      d.status >= 0 LEFT OUTER JOIN
                      dbo.syscomments e ON a.cdefault = e.id LEFT OUTER JOIN
                      sys.extended_properties g ON a.id = g.major_id AND a.colid = g.minor_id AND 
                      g.name = 'MS_Description' LEFT OUTER JOIN
                      sys.extended_properties f ON d.id = f.major_id AND f.minor_id = 0 AND 
                      f.name = 'MS_Description'
                where (g.class_desc is null or g.class_desc = 'OBJECT_OR_COLUMN')
                ORDER BY d.name, 字段序号
                                                  ";
        #endregion


    }
}
